package service

import (
	"MyBlogv2/common/HttpCode"
	define "MyBlogv2/common/define"
	"MyBlogv2/common/models"
	"MyBlogv2/common/redisUtil"
	"MyBlogv2/common/response"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Favorite(c *gin.Context) {
	typeOfFavorite, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")
	var dbCount int64
	models.Db.Model(new(models.Favorite)).Where("user_id = ? and type = ? and type_id = ?", userId, typeOfFavorite, typeId).Count(&dbCount)
	if dbCount > 0 {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	var favorite models.Favorite
	favorite.UserId = userId.(int)
	favorite.Type = typeOfFavorite
	favorite.TypeId = typeId
	models.Db.Model(new(models.Favorite)).Create(&favorite)
	redisUtil.AddFavoriteCount(typeId)
	response.ResponseOK(c, nil)
}

func CancelFavorite(c *gin.Context) {
	typeOfFavorite, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")
	var dbCount int64
	models.Db.Model(new(models.Favorite)).Where("user_id = ? and type = ? and type_id = ?", userId, typeOfFavorite, typeId).Count(&dbCount)
	if dbCount == 0 {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	redisUtil.DelFavoriteCount(typeId)
	models.Db.Model(new(models.Favorite)).Where("user_id = ? and type = ? and type_id = ?", userId, typeOfFavorite, typeId).Delete(new(models.Favorite))
	response.ResponseOK(c, nil)
}

func IsFavorite(c *gin.Context) {
	typeOfFavorite, _ := strconv.Atoi(c.Query("type"))
	typeId, _ := strconv.Atoi(c.Query("typeId"))
	userId, _ := c.Get("userId")
	var dbCount int64
	models.Db.Model(new(models.Favorite)).Where("user_id = ? and type = ? and type_id = ?", userId, typeOfFavorite, typeId).Count(&dbCount)
	if dbCount > 0 {
		response.ResponseOK(c, true)
		return
	}
	response.ResponseOK(c, false)
}

func GetFavoriteList4Admin(c *gin.Context) {
	var favoriteVoList []models.FavoriteListVo
	models.Db.Model(new(models.FavoriteListVo)).
		Table("t_favorite f").
		Select("f.*,u.username as username").
		Joins("left join sys_user u on f.user_id = u.id").
		Find(&favoriteVoList)
	for i, v := range favoriteVoList {
		if v.Type == define.FAVORITE_TYPE_ARTICLE {
			var article models.Article
			models.Db.Model(new(models.Article)).Where("id = ?", v.TypeId).First(&article)
			favoriteVoList[i].Content = article.ArticleContent
		} else if v.Type == define.FAVORITE_TYPE_LEAVE_WORD {
			var leaveWord models.LeaveWord
			models.Db.Model(new(models.LeaveWord)).Where("id = ?", v.TypeId).First(&leaveWord)
			favoriteVoList[i].Content = leaveWord.Content
		}
	}
	response.ResponseOK(c, favoriteVoList)
}

func SearchFavorite(c *gin.Context) {
	var favoriteVoList []models.FavoriteListVo
	var searchFavoriteDto models.SearchFavoriteDto
	if err := c.ShouldBind(&searchFavoriteDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	db := models.Db.Model(new(models.FavoriteListVo)).
		Table("t_favorite f").
		Select("f.*,u.username as username").
		Joins("left join sys_user u on f.user_id = u.id")
	if searchFavoriteDto.Username != "" {
		db.Where("u.username like ?", "%"+searchFavoriteDto.Username+"%")
	}
	if searchFavoriteDto.Type.Valid {
		db.Where("f.type = ?", searchFavoriteDto.Type.Int64)
	}
	if !time.Time(searchFavoriteDto.StartTime).IsZero() {
		db.Where("f.created_at >= ?", searchFavoriteDto.StartTime)
	}
	if !time.Time(searchFavoriteDto.EndTime).IsZero() {
		db.Where("f.created_at <= ?", searchFavoriteDto.EndTime)
	}
	if searchFavoriteDto.IsCheck.Valid {
		db.Where("f.is_check = ?", searchFavoriteDto.IsCheck.Int64)
	}
	db.Find(&favoriteVoList)
	for i, v := range favoriteVoList {
		if v.Type == define.FAVORITE_TYPE_ARTICLE {
			var article models.Article
			models.Db.Model(new(models.Article)).Where("id = ?", v.TypeId).First(&article)
			favoriteVoList[i].Content = article.ArticleContent
		} else if v.Type == define.FAVORITE_TYPE_LEAVE_WORD {
			var leaveWord models.LeaveWord
			models.Db.Model(new(models.LeaveWord)).Where("id = ?", v.TypeId).First(&leaveWord)
			favoriteVoList[i].Content = leaveWord.Content
		}
	}
	response.ResponseOK(c, favoriteVoList)
}

func UpdateFavoriteIsCheck(c *gin.Context) {
	var favoriteIsCheckDto models.FavoriteIsCheckDto
	if err := c.ShouldBind(&favoriteIsCheckDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.Favorite)).Where("id = ?", favoriteIsCheckDto.Id).Update("is_check", favoriteIsCheckDto.IsCheck)
	response.ResponseOK(c, nil)
}

func DeleteFavorite(c *gin.Context) {
	var ids []int
	if err := c.ShouldBindJSON(&ids); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.Favorite)).Where("id in (?)", ids).Delete(new(models.Favorite))
	response.ResponseOK(c, nil)
}
